package org.example;

import java.util.Stack;

public class Main {
    class Solution1 {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();

            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '{' || c == '[' || c == '(') {
                    stack.push(c);
                } else if (c == '}' || c == ']' || c == ')') {
                    if (stack.isEmpty()) {
                        return false;
                    }
                    char top = stack.pop();
                    if ((c == '}' && top != '{') ||
                            (c == ']' && top != '[') ||
                            (c == ')' && top != '(')) {
                        return false;
                    }
                }
            }
            return stack.isEmpty();
        }
    }

    public class Solution2 {
        public boolean IsPopOrder(int[] pushV, int[] popV) {
            if (pushV == null || popV == null || pushV.length != popV.length) {
                return false;
            }
            Stack<Integer> stack = new Stack<>();
            int popIndex = 0;
            for (int num : pushV) {
                stack.push(num);
                while (!stack.isEmpty() && stack.peek() == popV[popIndex]) {
                    stack.pop();
                    popIndex++;
                }
            }
            return stack.isEmpty();
        }
    }

    class Solution {
        public int evalRPN(String[] tokens) {
            Stack<Integer> stack = new Stack<>();
            for (String token : tokens) {
                if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                    int b = stack.pop();
                    int a = stack.pop();
                    switch (token) {
                        case "+":
                            stack.push(a + b);
                            break;
                        case "-":
                            stack.push(a - b);
                            break;
                        case "*":
                            stack.push(a * b);
                            break;
                        case "/":
                            stack.push(a / b);
                            break;
                    }
                } else {
                    stack.push(Integer.parseInt(token));
                }
            }
            return stack.pop();
        }
    }

    public static void main(String[] args) {


    }
}